<template>
	<div class="page">

		<div class="logo">
			<img class="logo-img" src="../assets/logo.png" alt="" />
			<div class="right-ic" @click="changeLanguage">
				<img src="../assets/100.png" alt="" />
				<span>{{$i18n.locale == 'zh' ? 'EN' : '中文'}}</span>
			</div>
		</div>
		
		<div class="top">
			<div class="search">
				<input type="text" v-model="search" :placeholder="$t('language.search')" />
				<img class="ic" src="../assets/101.png" alt="" />
			</div>
		</div>

		<div class="card">
			<div class="title" v-if="!hideCategory && category != ''">{{$i18n.locale == 'zh' ? category.cn_title: category.eu_title}}</div>
			<van-cell v-for="item in list" :key="item.id" :title="$i18n.locale == 'zh' ? item.cn_title: item.eu_title" is-link @click="goPage(item.id)" />
			<div class="not-text" v-if="list.length == 0">
				{{search == '' ?$t('language.postolder') : $t('language.normal')}}
			</div>
		</div>

	</div>
</template>

<script>
	import {
		getHelpList,
		getHelpSearch
	} from '@/api/user'
	let timer = null
	export default {
		name: 'List',
		data: function() {
			return {
				search: '',
				id: '',
				hideCategory: false,
				category: '',
				list: []
			}
		},
		watch: {
			search: function() {
				const t = this
				if (timer != null) clearTimeout(timer)
				if(t.search.length > 0) {
					t.hideCategory = true
				} else if(t.id != '') {
					t.hideCategory = false
					t.getList(t.id)
					return
				} else {
					return
				}
				timer = setTimeout(() => {
					timer = null
					clearTimeout(timer)
					t.getSearch()
				}, 500)
			}
		},
		created() {
			const {
				query
			} = this.$route;
			this.query = query
			if(query.id) {
				this.id = query.id
				this.getList(query.id)
			}
		},
		mounted() {},
		methods: {
			getList(id) {
				getHelpList({
					category_id: id
				}).then((res) => {
					if(res.data.length == 0) return
					const {
						category,
						list
					} = res.data
					this.category = category
					this.list = list
					console.log(res)
				}).catch((err) => {
					console.log(err)
				})
			},
			getSearch() {
				getHelpSearch({
					key: this.search
				}).then((res) => {
					console.log(res)
					this.list = res.data
				}).catch((err) => {
					console.log(err)
				})
			},
			goPage(id) {
				this.$router.push({
					path: 'details',
					query: {
						id
					}
				})
			},
			changeLanguage() {
				if (this.$i18n.locale == 'zh') {
					this.$i18n.locale = 'en';
					this.$cookies.set('locale', 'en')
				} else {
					this.$i18n.locale = 'zh';
					this.$cookies.set('locale', 'zh')
				}
			}
		}
	};
</script>
<style type="text/css" lang="scss" scoped>
	.page {
		background-color: #F5F5FA;
	}

	.top {
		height: 216px;
		background: #060143;
	}

	.search {
		position: relative;
		z-index: 10;
		padding: 0 28px;
		margin: 0 30px;
		height: 88px;
		background-color: #fff;
		border: 1px solid #B78D5B;
		box-sizing: border-box;
		border-radius: 15px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-family: Source Han Sans CN;
		font-weight: 400;
	
		input {
			font-size: 28px;
			color: #333333;
			width: 100%;
			height: 100%;
		}
	
		.ic {
			width: 28px;
			height: 28px;
			flex-shrink: 0;
		}
	}

	.card {
		position: relative;
		top: -100px;
		z-index: 999;
		margin: 30px 30px;
		padding: 38px 30px;
		z-index: 10;
		border-radius: 15px;
		background-color: #fff;
		.title {
			font-weight: bold;
			color: #000000;
			padding-left: 16px;
			position: relative;
			font-family: Source Han Sans CN;
			font-weight: bold;
			font-size: 38px;
			color: #C99D6A;

			&::after {
				content: '';
				position: absolute;
				top: 0;
				left: 0;
				width: 6px;
				height: 100%;
				border-radius: 4px;
				background-color: #C99D6A;
			}
		}

		.van-cell {
			padding: 36px 0;
			font-family: Source Han Sans CN;
			font-weight: 500;
			font-size: 28px;
			line-height: 28px;
			color: #242933;
			align-items: center;

			.van-cell__left-icon,
			.van-cell__right-icon {
				font-size: 30px;
			}

			&::after {
				left: 0;
				border-bottom-color: #ccc;
			}
		}

	}
	.not-text {
		font-size: 24px;
		color: #999999;
		text-align: center;
	}
</style>